
import { ExcavationTerrain } from '@/assets/js/scene/ExcavationTerrain'
import { flyRoute } from '@/assets/js/scene/flyRoute'
//import { terrainSlopeAnalysis } from '@/assets/js/scene/terrainSlopeAnalysis'
import { isolineAnalysis } from '@/assets/js/scene/isolineAnalysis'
import { sightlineAnalysis } from '@/assets/js/scene/sightlineAnalysis'
import { viewshed3DAnalysis } from '@/assets/js/scene/viewshed3DAnalysis'
import { skylineAnalysis } from '@/assets/js/scene/skylineAnalysis'

class SceneTool {
  constructor(tempviewer) {
    // 初始化测量距离
    this.viewer = tempviewer
    this.handlerDis = new Cesium.MeasureHandler(this.viewer, Cesium.MeasureMode.Distance, 0)
    // 初始化测量面积
    this.handlerArea = new Cesium.MeasureHandler(this.viewer, Cesium.MeasureMode.Area, 0)
    // 注册测距功能事件
    this.handlerDis.measureEvt.addEventListener((result) => {
      var dis = Number(result.distance)
      var distance = dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm'
      this.handlerDis.disLabel.text = '距离:' + distance
    })
    this.handlerDis.activeEvt.addEventListener((isActive) => {
      if (isActive === true) {
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
      } else {
        this.viewer.enableCursorStyle = true
      }
    })
    this.handlerArea.measureEvt.addEventListener((result) => {
      var mj = Number(result.area)
      var area = mj > 1000000 ? (mj / 1000000).toFixed(2) + 'km²' : mj.toFixed(2) + '㎡'
      this.handlerArea.areaLabel.text = '面积:' + area
    })
    this.handlerArea.activeEvt.addEventListener((isActive) => {
      if (isActive === true) {
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
      } else {
        this.viewer.enableCursorStyle = true
      }
    })

    this.Excavation = new ExcavationTerrain(tempviewer)
    this.flyRoute = new flyRoute(tempviewer)
    //this.terrainSlopeAnalysis = new terrainSlopeAnalysis(tempviewer)
    this.isolineAnalysis = new isolineAnalysis(tempviewer)
    this.sightlineAnalysis = new sightlineAnalysis(tempviewer)
    this.viewshed3DAnalysis = new viewshed3DAnalysis(tempviewer)
    this.skylineAnalysis = new skylineAnalysis(tempviewer)

    this.showFlyRouteModule = false
    this.flyCircleDrawHandler = null
    this.flyCirclePoint = null
    this.bbindSliderEvt = false
  }

  /**
   * 注销量算状态
   */
  deactiveAll() {
    this.handlerDis && this.handlerDis.deactivate()
    this.handlerArea && this.handlerArea.deactivate()
  }

  /**
   * 距离量算
   */
  measureDis() {
    this.deactiveAll()
    this.handlerDis && this.handlerDis.activate()
  }

  /**
   * 面积量算
   */
  measureArea() {
    this.deactiveAll()
    this.handlerArea && this.handlerArea.activate()
  }

  /**
   * 量算清除
   */
  clearAll() {
    this.handlerDis && this.handlerDis.clear()
    this.handlerArea && this.handlerArea.clear()
  }

  /**
   * 地表透明度设置
   */
  setSceneGlobeAlpha(value) {
    this.viewer.scene.globe.globeAlpha = value
  }

  /**
   * 是否开启地下
   */
  setSceneUnderground(b) {
    this.viewer.scene.undergroundMode = b
    if (b) {
      this.viewer.scene.screenSpaceCameraController.minimumZoomDistance = -1000
    } else {
      this.viewer.scene.screenSpaceCameraController.minimumZoomDistance = 1
    }
  }

  /**
   *设置地表透明度
   */
  setGlobeAlpha(value) {
    this.viewer.scene.globe.globeAlpha = value
  }

  /**
   *地面开挖
   */
  excavationTer() {
    this.Excavation.excavation()
  }
  /**
   *清除开挖
   */
  clearExcavationTer() {
    this.Excavation.remove()
  }

  /**
   * 路径飞行
   */
  startPlay() {
    this.flyRoute.play()
  }

  /**
   * 暂停飞行
   */
  pause() {
    this.flyRoute.pause()
  }

  /**
   * 停止飞行
   */
  stop() {
    this.flyRoute.stop()
  }
    
  
  /**
   * 设置影像卷帘模式
   */
  setImageryRollerMode(b) {
    const imageryLayers = this.viewer.imageryLayers
    if (b) {
      for (let i = 0; i < imageryLayers.length; i++) {
        const imageryLayer = imageryLayers.get(i)
        switch (this.rollerShutterConfig.splitDirection) {
          case Cesium.SplitDirection.LEFT:
            imageryLayer.splitDirection = new Cesium.Cartesian2(Cesium.ImagerySplitDirection.RIGHT, Cesium.ImagerySplitDirection.NONE)
            break
          case Cesium.SplitDirection.RIGHT:
            imageryLayer.splitDirection = new Cesium.Cartesian2(Cesium.ImagerySplitDirection.LEFT, Cesium.ImagerySplitDirection.NONE)
            break
          case Cesium.SplitDirection.TOP:
            imageryLayer.splitDirection = new Cesium.Cartesian2(Cesium.ImagerySplitDirection.NONE, Cesium.ImagerySplitDirection.BOTTOM)
            break
          case Cesium.SplitDirection.BOTTOM:
            imageryLayer.splitDirection = new Cesium.Cartesian2(Cesium.ImagerySplitDirection.NONE, Cesium.ImagerySplitDirection.TOP)
            break
          default:
            imageryLayer.splitDirection = new Cesium.Cartesian2(Cesium.ImagerySplitDirection.NONE, Cesium.ImagerySplitDirection.NONE)
            break
        }
      }
    } else {
      for (let i = 0; i < imageryLayers.length; i++) {
        const imageryLayer = imageryLayers.get(i)
        imageryLayer.splitDirection = new Cesium.Cartesian2(Cesium.ImagerySplitDirection.NONE, Cesium.ImagerySplitDirection.NONE)
      }
    }
  }

  /**
   * 初始化卷帘。设置分割条初始位置及绑定相关事件。
   */
  initRollerShutter() {
    const windowWidth = document.body.clientWidth // 窗口宽度
    const windowHeight = document.body.clientHeight // 窗口高度
    this.rollerShutterConfig = { // 卷帘配置参数，以对象方式实现地址传递
      splitDirection: Cesium.SplitDirection.LEFT,
      verticalSplitPosition: windowWidth / 2,
      horizontalSplitPosition: windowHeight / 2,
      splitLayers: [], // 参与卷帘的S3M图层名
      wholeLayers: [], // 所有S3M图层的名称
      latestSplitDirection: null // 用于在禁用卷帘后恢复之前的卷帘方向
    }

    //this.setRollerShutterSplit();
    //this.bindSliderEvt();
  }

  /**
   * 注册卷帘分割条的拖拽事件。
   *
   */
  bindSliderEvt() {
    var verticalSlider = document.getElementById('vertical-slider')
    var horizontalSlider = document.getElementById('horizontal-slider')
    verticalSlider.addEventListener('mousedown', mouseDown, false)
    horizontalSlider.addEventListener('mousedown', mouseDown, false)
     const windowHeight = document.body.clientHeight
    var me = this
    document.body.addEventListener('mouseup', mouseUp, false)

    function mouseUp(e) {
      document.body.removeEventListener('mousemove', sliderMove, false)
    }

    function mouseDown(e) {
      document.body.addEventListener('mousemove', sliderMove, false)
    }

    function sliderMove(e) { // 鼠标拖拽时执行
      // 解决拖拽鼠标粘滞的问题
      if (e.preventDefault) {
        e.preventDefault()
      } else {
        e.returnValue = false
      }
      if (me.rollerShutterConfig.splitDirection === Cesium.SplitDirection.LEFT || me.rollerShutterConfig.splitDirection === Cesium.SplitDirection.RIGHT) {
        verticalSlider.style.left = e.clientX + 'px'
        me.rollerShutterConfig.verticalSplitPosition = e.clientX
      } else if (me.rollerShutterConfig.splitDirection === Cesium.SplitDirection.TOP || me.rollerShutterConfig.splitDirection === Cesium.SplitDirection.BOTTOM) {
        let clientY = e.clientY
        if (clientY < 0) {
          clientY = 0
        } else if (clientY > windowHeight) {
          clientY = windowHeight - document.getElementById('horizontal-slider').clientHeight
        }
        horizontalSlider.style.top = (clientY) + 'px'
        me.rollerShutterConfig.horizontalSplitPosition = windowHeight - clientY
      }
      me.setRollerShutterSplit()
    }
  }
  /**
   * 设置卷帘的分割方向及分割条的位置。
   */
  setRollerShutterSplit() {
    let splitPosition = null
    if (this.rollerShutterConfig.splitDirection === Cesium.SplitDirection.LEFT || this.rollerShutterConfig.splitDirection === Cesium.SplitDirection.RIGHT) {
      splitPosition = this.rollerShutterConfig.verticalSplitPosition
      this.viewer.scene.imagerySplitPosition.x = this.rollerShutterConfig.verticalSplitPosition / document.body.clientWidth
    } else if (this.rollerShutterConfig.splitDirection === Cesium.SplitDirection.TOP || this.rollerShutterConfig.splitDirection === Cesium.SplitDirection.BOTTOM) {
      splitPosition = this.rollerShutterConfig.horizontalSplitPosition
      this.viewer.scene.imagerySplitPosition.y = this.rollerShutterConfig.horizontalSplitPosition / document.body.clientHeight
    }
    for (const layer of this.viewer.scene.layers.layerQueue) {
     
      layer.splitDirection = this.rollerShutterConfig.splitDirection
      if (splitPosition && layer._name.indexOf('管线') < -1) { // 如果禁用卷帘就没有必要设置分割位置
        console.log(layer._name)
        layer.splitPosition = splitPosition
      }
    }
  }
  /**
   * 设置卷帘的分割方向及分割条的位置。
   *
   */
  changeSwitchType(switchType, switchDirection) {
    if (!this.bbindSliderEvt) {
      this.bbindSliderEvt = true
      this.initRollerShutter()
     this.setRollerShutterSplit()
      this.bindSliderEvt()
    }
    if (switchType === '场景卷帘') {
      this.setImageryRollerMode(true)
      const verticalSlider = document.getElementById('vertical-slider')
      const horizontalSlider = document.getElementById('horizontal-slider')
      if (switchDirection === '横向卷帘') {
        verticalSlider.style.display = 'block'
        horizontalSlider.style.display = 'none'
        this.rollerShutterConfig.splitDirection = Cesium.SplitDirection.LEFT
      } else {
        verticalSlider.style.display = 'none'
        horizontalSlider.style.display = 'block'
        this.rollerShutterConfig.splitDirection = Cesium.SplitDirection.BOTTOM
      }
      this.setRollerShutterSplit()
    } else {
         


    }
  }
  /**
   * 设置卷帘的分割方向及分割条的位置。
   *
   */
  changesSwitchDirection(switchDirection) {
    const verticalSlider = document.getElementById('vertical-slider')
    const horizontalSlider = document.getElementById('horizontal-slider')
    if (switchDirection === '横向卷帘') {
      verticalSlider.style.display = 'block'
      horizontalSlider.style.display = 'none'
      this.rollerShutterConfig.splitDirection = Cesium.SplitDirection.LEFT
    } else {
      verticalSlider.style.display = 'none'
      horizontalSlider.style.display = 'block'
      this.rollerShutterConfig.splitDirection = Cesium.SplitDirection.BOTTOM
    }
    this.setImageryRollerMode(true)
    this.setRollerShutterSplit()
  }
  /**
   * 取消卷帘。
   *
   */
  cancelSwitch() {
    const verticalSlider = document.getElementById('vertical-slider')
    const horizontalSlider = document.getElementById('horizontal-slider')
    this.rollerShutterConfig.latestSplitDirection = this.rollerShutterConfig.splitDirection
    this.rollerShutterConfig.splitDirection = Cesium.SplitDirection.NONE
    verticalSlider.style.display = 'none'
    horizontalSlider.style.display = 'none'
    this.setImageryRollerMode(false)
    this.setRollerShutterSplit();
  }

  /**
   * 绕物旋转。
   *
   */
  Spin() {
    if (!this.flyCircleDrawHandler) {
      this.flyCircleDrawHandler = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Point)
      this.flyCircleDrawHandler.activeEvt.addEventListener((isActive) => {
        if (isActive === true) {
          this.viewer.enableCursorStyle = false
          this.viewer._element.style.cursor = ''
          if (document.getElementById('scene').classList.contains('drawCur')) {
            document.getElementById('scene').classList.remove('drawCur')
          }
          document.getElementById('scene').classList.add('drawCur')
        } else {
          this.viewer.enableCursorStyle = true
          document.getElementById('scene').classList.add('drawCur')
        }
      })
      this.flyCircleDrawHandler.drawEvt.addEventListener((result) => {
        this.flyCirclePoint = result.object.position
        this.viewer.entities.removeById('fly-circle-point')
        this.viewer.entities.add({
          id: 'fly-circle-point',
          position: this.flyCirclePoint,
          billboard: {
            image: '../../../static/img/scene/flyCircle.png',
            scaleByDistance: new Cesium.NearFarScalar(10, 1.0, 1000, 0.1),
            disableDepthTestDistance: Number.POSITIVE_INFINITY // 关闭深度检测，使billboard不至于被裁剪
          }
        })
        this.viewer.scene.camera.stopFlyCircle() // 先停止之前的旋转，再开始新的旋转
        this.viewer.scene.camera.flyCircle(this.flyCirclePoint)
        this.flyCircleDrawHandler.clear()
      })
    }
    this.flyCircleDrawHandler.activate()
  }

  /**
   * 取消绕物旋转。
   *
   */
  cancelSpin() {
    this.viewer.scene.camera.stopFlyCircle()
    this.viewer.entities.removeById('fly-circle-point')
    this.flyCirclePoint = null
  }
}

export { SceneTool }
